﻿var Local = {
	/**
	 * 获取文件文本
	 * @param {String} filename 文件名
	 */
	getFileText: function(filename) {
		var fso = new ActiveXObject("Scripting.FileSystemObject");
		if (!fso.FileExists(filename)) return "";
		var file = fso.GetFile(filename);
		if (file.Size <= 0) return "";
		var istream = fso.OpenTextFile(filename, 1);
		if (!istream) return "";
		try {
			var contents = istream.ReadAll();
			istream.Close();
		} catch (ex) {
			alert(ex.message);
		} finally {
			istream = null;
			fso = null;
		}
		return contents;
	},
	specialFolders: function(name) {
		var shell = new ActiveXObject("WScript.Shell");
		try {
			var path = shell.SpecialFolders(name);
		} catch (ex) {
			alert(ex.message);
		} finally {
			shell = null;
		}
		return path;
	},
	/**
	 * 设置文件文本
	 * @param {String} filename 文件名
	 * @param {String} text 文本内容
	 * @param {Boolean} append 是否为追加模式
	 */
	setFileText: function (filename, text, append) {
		var fso = new ActiveXObject("Scripting.FileSystemObject"); // 文件系统
		var istream = fso.OpenTextFile(filename, append ? 8 : 2, true);
		if (!istream) return;
		try {
			istream.write(text);
			istream.Close();
			return true;
		} catch (ex) {
			alert(ex.message);
		} finally {
			istream = null;
			fso = null;
		}
	},
	/**
	 * 读取文件
	 * @param {String} filename 文件名
	 * @param {String} charset 字符集合
	 */
	readFileText: function(filename, charset) {
		var adostream = new ActiveXObject("ADODB.Stream");
		adostream.Type = 2; //TextStreamType;
		adostream.Charset = "utf-8";
		adostream.Open();
		adostream.LoadFromFile(filename);
		var contents = adostream.ReadText();
		adostream.Close();
		adostream = null;
		return contents;
	},
	/**
	 * 保存utf-8文件 无BOM
	 * @param {String} filename 文件名
	 * @param {String} text 文本
	 * @param {String} charset 默认utf-8
	 */
	writeFileText: function(filename, text, charset) {
		var adostream = new ActiveXObject("ADODB.Stream");
		adostream.Type = 2; //TextStreamType;
		adostream.Charset = "utf-8";
		adostream.Open();
		adostream.WriteText(text);
		var adostream2 = new ActiveXObject("ADODB.Stream");
		adostream2.Type = 1;
		adostream2.Open();
		adostream.Position = 3; // Remove BOM
		adostream.CopyTo(adostream2);
		adostream2.Position = 0;
		adostream2.SaveToFile(filename, 2);
		adostream2.Close();
		adostream2 = null;

		adostream.Close();
		adostream = null;
	},
	/**
	 * 文件是否存在
	 * @param {String} filename 文件名
	 */
	fileExists: function(filename) { // 文件是否存在
		if (!filename) return;
		var fso = new ActiveXObject('Scripting.FileSystemObject');
		try {
			return fso.FileExists(filename);
		} catch (ex) {
			alert(ex.message);
		} finally {
			fso = null;
		}
	},
	/**
	 * 目录是否存在
	 * @param {String} foldername 目录名
	 */
	folderExists: function(foldername) { 
		if (!foldername) return;
		var fso = new ActiveXObject('Scripting.FileSystemObject');
		try {
			return fso.FolderExists(foldername);
		} catch (ex) {
			alert(ex.message);
		} finally {
			fso = null;
		}
	},
	/**
	 * 创建目录
	 * @param {String} foldername 目录名
	 */
	createFolder: function(foldername) { // 目录是否存在
		if (!foldername) return;
		var fso = new ActiveXObject('Scripting.FileSystemObject');
		try {
			return fso.CreateFolder(foldername);
		} catch (ex) {
			alert(ex.message);
		} finally {
			fso = null;
		}
	},
	/**
	 * 获得文件扩展名
	 * @param {String} path 路径字符串
	 */
	getExtension: function(path) {
		var match = (/(\.[^\.^\\^\/]+)$/g).exec(path);
		return match ? match[1] : '';
	},
	/**
	 * 获得文件名
	 * @param {String} path 路径字符串
	 */
	getFileName: function(path) {
		var match = (/([^\\^\/^:]+)$/g).exec(path);
		return match ? match[1] : '';
	},
	/**
	 * 获得文件路径
	 * @param {String} path 路径字符串
	 */
	getFilePath: function(path) {
		var match = (/(.*?)[^\\^\/]+$/g).exec(path);
		return match ? match[1] : '';
	},
	/**
	 * 文件对话框
	 * @param {String} title 标题
	 */
	folderDialog: function(title) {
		var shell = new ActiveXObject('Shell.Application');
		try {
			var folder = shell.BrowseForFolder(0, title, 0); 
			if (folder) return folder.Items().Item().Path;
		} catch (ex) {
			alert(ex.message);
		} finally {
			shell = null;
		}
	},
	/**
	 * 删除文件
	 * @param {String} filename 文件名
	 */
	deleteFile: function(filename) {
		var fso = new ActiveXObject('Scripting.FileSystemObject');
		try {
			fso.DeleteFile(filename); 
		} catch (ex) {
			alert(ex.message);
		} finally {
			fso = null;
		}
	},
	/**
	 * 目录改名
	 * @param {String} oldFolder 原目录名
	 * @param {String} newFolder 现目录名
	 */
	moveFolder: function(oldFolder, newFolder) {
		var fso = new ActiveXObject('Scripting.FileSystemObject');
		try {
			fso.MoveFolder(oldFolder, newFolder); 
		} catch (ex) {
			alert(ex.message);
		} finally {
			fso = null;
		}
	},
	/**
	 * 文件改名
	 * @param {String} oldFile 原文件名
	 * @param {String} newFile 现文件名
	 */
	moveFile: function(oldFile, newFile) {
		var fso = new ActiveXObject('Scripting.FileSystemObject');
		try {
			fso.MoveFile(oldFile, newFile); 
		} catch (ex) {
			alert(ex.message);
		} finally {
			fso = null;
		}
	},
	/**
	 * 获得文件路径 file:///c:\temp -> c:\temp 
	 * @param {String} url 链接地址
	 */
	absFileName: function(url) {
		return url.replace(/^file\:\/\/\//g, '').replace(/\//g, '\\');
	},
	/**
	 * 扫描目录的文件
	 * @param {String} path 目录路径
	 * @param {Function} fileScaner 扫描文件的方法 
	 */
	scanFolder: function(path, fileScaner, folderScaner) {
		if (!path) return;
		var fso = new ActiveXObject('Scripting.FileSystemObject');
		try {
			var folder = fso.GetFolder(path);
			if (fileScaner) { // 如果需要扫描文件
				for (var files = new Enumerator(folder.Files); !files.atEnd(); files.moveNext()) {
					if (fileScaner("" + files.item())) 
						return true;
				}
			}
			for (var folders = new Enumerator(folder.SubFolders); !folders.atEnd(); folders.moveNext()) {
				if (folderScaner) folderScaner("" + folders.item());
				if (this.scanFolder(folders.item(), fileScaner, folderScaner)) return true;
			}
		} catch(e) {
			alert(e.message);
		} finally {
			fso = null;
		}
	},
	/**
	 * 异步请求
	 * @param {String} url 请求连接
	 * @param {String} type 请求类型GET、POST
	 * @param {String} data 请求数据
	 * @param {String} cookie
	 * @param {Function} loaded 加载完毕的事件
	 * @param {String} referer
	 * @param {String} proxy 代理地址
	 */
	request: function(url, type, data, cookie, loaded, referer, proxy){
		if (!loaded) return;
		var xmlhttp = new ActiveXObject("WinHttp.WinHttpRequest.5.1");
		type = typeof type == "string" ? type : "POST";
		xmlhttp.open(type, url, false);
		if (proxy) xmlhttp.setProxy(2, proxy);
		if (cookie) xmlhttp.setRequestHeader("Cookie", cookie);
		if (!referer) referer = url;
		xmlhttp.setRequestHeader("Referer", referer); 
		xmlhttp.setRequestHeader("Accept-Charset", "utf-8;");
		xmlhttp.setRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;");
		if (typeof data == "string") {
			xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
			xmlhttp.setRequestHeader("Content-Length", data.length);
		}
		try {
			xmlhttp.send(data);
			loaded(xmlhttp);
		} catch(ex) {
			loaded({});
		}
	}, 
	bytesEncode: function(bytes, encoding) {
		var stream = new ActiveXObject("adodb.stream")
		with (stream) {
			Type = 2; // adTypeBinary = 1; adTypeText = 2;
			Open();
			WriteText(bytes);
			Position = 0;
			Charset = encoding;
			Position = 2; // BOM
			return ReadText();
		}
	},
	getUrlText: function(url, encoding) {
		var self = this;
		var result = "";
		this.request(url, "GET", null, null, function(xmlhttp) {
			if (encoding)
				result = self.bytesEncode(xmlhttp.responseBody, encoding);
			else result = xmlhttp.responseText;
		});
		return result;
	},
	/**
	 * 创建快捷方式
	 * @param {String} filename 文件名
	 * @param {String} target 目标程序
	 * @param {String} arguments 执行参数
	 * @param {String} wroking 工作路径
	 */
	createShortcut: function(filename, target, arguments, wroking) {
		var shell = new ActiveXObject("WScript.Shell");
		var link = shell.CreateShortcut(filename);
		link.TargetPath = target;
		link.arguments = arguments; 
		link.WorkingDirectory = wroking;
		link.Save();
		link = null;
		shell = null;
	},
	commandLine: function(command) {
		var shell = new ActiveXObject("WScript.Shell");
		shell.Run(command, 5, true); // 执行并等待
		shell = null;
	},
	/**
	 * 下载文件
	 * @param {String} 目标链接
	 * @param {String} 下载文件路径
	 */ 
	download: function(url, filename) {
		this.request(url, "GET", null, null, function(xmlhttp) {
			var adodbStream = new ActiveXObject("ADODB.Stream");
			adodbStream.Type = 1;
			adodbStream.Open(); 
			adodbStream.Write(xmlhttp.responseBody);
			adodbStream.SaveToFile(filename, 2); 
			adodbStream.Close(); 
			adodbStream = null;
		}); 
	}
};